package com.ronri.ams;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.utils.GoHomeUtil;
import com.ronri.common.UrlEncode;
import com.ronri.db.RemoteDataService;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/13.
 */

public class activity_chouyang extends Activity implements AdapterView.OnItemClickListener {

    private TextView title;
    private RelativeLayout btnBack;
    private RelativeLayout btnMeanMore;

    private RelativeLayout ml1 = null;
    private RelativeLayout ml2 = null;
    private RelativeLayout ml3 = null;
    private RelativeLayout ml4 = null;

    private LinearLayout mscyrw = null;
    private LinearLayout mscyhz = null;
    private LinearLayout mscjjg = null;

    private TextView mcyrw = null;
    private TextView mcyhz = null;
    private TextView mcjjg = null;

    @Override
    protected void onDestroy() {

        InitApplication initApplication = (InitApplication) getApplicationContext();

        initApplication.getActivities().remove(this);

        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        InitApplication initApplication = (InitApplication) getApplicationContext();

        initApplication.getActivities().add(this);

        setContentView(R.layout.activity_chouyang);

        initView();

        loadRightTitle();

        getCverRemoteData();
    }

    /**
     * 加载去主页的按钮
     */
    public void loadRightTitle() {

        View view = GoHomeUtil.createPopView(getApplicationContext(),
                new String[]{"主页"});

        // 弹出更多的菜单
        final PopupWindow pop = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);

        // 需要设置一下此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());

        // 设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);

        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);

        final Button btn01 = (Button) pop.getContentView().findViewWithTag(
                "btn0");

        View.OnClickListener menuOnClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (v == btn01) {
                    GoHomeUtil.goHome(getApplicationContext());

                }
            }
        };

        btn01.setOnClickListener(menuOnClick);

        btnMeanMore.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (pop.isShowing()) {
                    // 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
                    pop.dismiss();
                } else {
                    // 显示窗口
                    pop.showAsDropDown(v);
                }

            }
        });
    }

    public void initView() {
        title = findViewById(R.id.tvTitle);
        btnBack = findViewById(R.id.btnBack);
        btnMeanMore = findViewById(R.id.btnMeanMore);

        ml1 = findViewById(R.id.l1);
        ml2 = findViewById(R.id.l2);
        ml3 = findViewById(R.id.l3);
        ml4 = findViewById(R.id.l4);
        mscyrw = findViewById(R.id.scyrw);
        mscyhz = findViewById(R.id.scyhz);
        mscjjg = findViewById(R.id.scjjg);

        mcyrw = findViewById(R.id.tvcyrw);
        mcjjg = findViewById(R.id.tvjg);
        mcyhz = findViewById(R.id.tvcyhz);


        if (title != null)
            title.setText("样品抽检");

        if (btnBack != null) {
            btnBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
                }
            });
        }

        mcyrw.setOnClickListener(onClickListener);
        mcyrw.setTag(8);
        mcjjg.setOnClickListener(onClickListener);
        mcjjg.setTag(9);
        mcyhz.setOnClickListener(onClickListener);
        mcyhz.setTag(10);

        ml1.setOnClickListener(onClickListener);
        ml1.setTag(1);
        ml2.setOnClickListener(onClickListener);
        ml2.setTag(2);
        ml3.setOnClickListener(onClickListener);
        ml3.setTag(3);
        ml4.setOnClickListener(onClickListener);
        ml4.setTag(4);

        mscyrw.setOnClickListener(onClickListener);
        mscyrw.setTag(5);
        mscyhz.setOnClickListener(onClickListener);
        mscyhz.setTag(6);
        mscjjg.setOnClickListener(onClickListener);
        mscjjg.setTag(7);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int nPoint = (int) view.getTag();
            switch (nPoint) {
                case 5:
                case 1: {
                    // 生产主体
                    Intent intent = new Intent(InitApplication.getContext(), com.ronri.ams.task.activity_TaskList.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("type", 0);
                    intent.putExtras(bundle);
                    //跳转Activity
                    startActivityForResult(intent, 1501);
                }
                break;
                case 6:
                case 2: {
                    Intent intent = new Intent(InitApplication.getContext(), com.ronri.ams.task.activity_sample_summray.class);

                    Bundle bundle = new Bundle();
                    bundle.putInt("type", 1);
                    intent.putExtras(bundle);

                    //跳转Activity
                    startActivityForResult(intent, 1502);
                }
                break;
                case 7:
                case 3: {
                    // 生产主体
                    Intent intent = new Intent(InitApplication.getContext(), com.ronri.ams.task.activity_detect_summray.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("type", 2);
                    intent.putExtras(bundle);
                    //跳转Activity
                    startActivityForResult(intent, 1503);
                }
                break;
                case 4: {
                    //                    View view1 = new View()
                    //                    ToastUtil toastUtil=new ToastUtil(mContext, );
                    //                    toastUtil.Short(mContext,"正在开发中...")
                    //                            .setToastBackground(Color.WHITE,R.drawable.toast_radius)
                    //                            //.setToastColor(Color.WHITE, getResources().getColor(R.color.colorAccent))
                    //                            .show();
                }
                break;
            }
        }
    };

    /***
     * 点击后跳转
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position) {
            case 0: {
                // 生产主体
                Intent intent = new Intent(this, com.ronri.ams.task.activity_TaskList.class);
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                intent.putExtras(bundle);
                //跳转Activity
                startActivityForResult(intent, 1501);
            }
            break;
            case 1: {
                // 生产主体
                Intent intent = new Intent(this, com.ronri.ams.task.activity_sample_summray.class);

                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                intent.putExtras(bundle);

                //跳转Activity
                startActivityForResult(intent, 1502);
            }
            break;
            case 2: {
                // 生产主体
                Intent intent = new Intent(this, com.ronri.ams.task.activity_detect_summray.class);
                Bundle bundle = new Bundle();
                bundle.putInt("type", 2);
                intent.putExtras(bundle);
                //跳转Activity
                startActivityForResult(intent, 1503);
            }
            break;
            default:
                break;
        }
    }

    /****
     * 获取远程数据
     */
    public void getCverRemoteData() {

        RemoteDataService remoteDataService = new RemoteDataService();

        // 获取远程地址
        String szRemoteAddress = InitApplication.SystemConfig.getBase().RemoteAddress(
                InitApplication.SystemConfig.getModule().getWelcome().getAddress());

        Map<String, Object> mParam = new HashMap<String, Object>();

        // 获取远程数据
        remoteDataService.getRemoteData(szRemoteAddress,
                UrlEncode.getUrlParamsByMap(mParam, false),
                InitApplication.SystemConfig.getModule().getWelcome().getMethod(),
                new RemoteDataService.OnRemoteDataEvent() {
                    // 返回结果
                    @Override
                    public void OnResultEvent(String szString) {
                        if (szString.isEmpty())
                            return;

                        try {
                            JSONObject jsonObject = JSON.parseObject(szString);

                            if (jsonObject.getBoolean("success") == true) {
                                JSONObject jdata = jsonObject.getJSONObject("data");

                                if (jdata != null) {
                                    mcyrw.setText(jdata.getString("task"));
                                    mcyhz.setText(jdata.getString("pool"));
                                    mcjjg.setText(jdata.getString("result"));
                                }
                            } else {
                                // 提示错误信息
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }

                    // 状态控制，是否显示加载过程
                    @Override
                    public void OnRuningState(int nStatus) {
                        if (nStatus == SystemEnvironmental.StartGetDataFromServer) {
                            //WaitDialog.ShowDialog(mContext, "请稍后...");
                        } else {
                            //WaitDialog.HideDialog();
                        }
                    }

                    @Override
                    public String OnGetCustomParam() {
                        return null;
                    }
                }
        );
    }
}
